//
//  CreateContentViewImageCell.swift
//  iModern
//
//  Created by Ralf on 5/29/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

import UIKit

class CreateContentViewImageCell: UITableViewCell, UIScrollViewDelegate
{
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let screen      = UIScreen.mainScreen().bounds

    var images = [UIImage]()
    var scrollView:  UIScrollView!
    var pageControl: UIPageControl!

    var textView: UITextView!

    var descriptions = [String]()

    override func awakeFromNib()
    {
        super.awakeFromNib()

        initScrollView()
        initPageControl()
        initTextView()
    }

    override func setSelected(selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)
    }

    func initScrollView()
    {
        if scrollView == nil
        {
            let frame = CGRectMake(0, 0, screen.size.width, screen.size.width)
            scrollView = UIScrollView(frame: frame)

            scrollView!.pagingEnabled = true
            scrollView!.showsHorizontalScrollIndicator = false
            scrollView!.showsVerticalScrollIndicator = false
            scrollView!.scrollsToTop = false
            scrollView.delegate = self

            self.addSubview(scrollView!)
        }
    }

    func initPageControl()
    {
        if pageControl == nil
        {
            pageControl = UIPageControl()

            self.pageControl.frame = CGRectMake(0, screen.size.width - 30, self.bounds.width, 20)
            self.pageControl.pageIndicatorTintColor = UIColor.grayColor()
            self.pageControl.backgroundColor = UIColor.clearColor()
            self.pageControl.currentPageIndicatorTintColor = UIColor.whiteColor()

            self.addSubview(pageControl!)
        }
    }

    func initTextView()
    {
        if textView == nil
        {
            let frame = CGRectMake(0, screen.width, screen.width, 100)
            textView = UITextView(frame: frame)
            textView.font = UIFont.systemFontOfSize(14)
            textView.text = "文字描述"
            self.addSubview(textView)
        }
    }

    func addImage(image: UIImage)
    {

        images.append(image)

        let width = screen.size.width * CGFloat(images.count)

        scrollView.contentSize = CGSizeMake(width, 0)

        let imageView = UIImageView(image: image)
        let x         = CGFloat(images.count - 1) * screen.size.width
        imageView.frame = CGRectMake(x, 0.0, screen.size.width, screen.size.width)
        scrollView.addSubview(imageView)

        pageControl.numberOfPages = images.count
        scrollView.contentOffset = CGPointMake(x, 0.0)
    }

    func handleTapGesture(sender: UITapGestureRecognizer)
    {
        images.removeAtIndex(1)
    }

    func scrollViewDidScroll(scrollView: UIScrollView)
    {
        let x = CGFloat(scrollView.contentOffset.x)
        let y = CGFloat(scrollView.frame.size.width / 2)

        let page = Int((x + y) / scrollView.frame.size.width)
        pageControl.currentPage = page
    }
}
